<?php
	/**
	 * Created by PhpStorm.
	 * User: mybook-lhp
	 * Date: 18/6/7
	 * Time: 上午9:59
	 */

	namespace app\store\api;


	use app\common\controller\API;
	use app\common\utils\UtilImgeProductQrcode;
	use app\common\utils\UtilStr;
	use app\store\logic\EasyWechat;
	use app\store\logic\Img;


	class Diffuse extends API
	{
		protected $miniapp = null;

		public function _initialize()
		{
			parent::_initialize();
			$config = Mbpayment::getPayMentConfig(MALL_APP_ID);
			EasyWechat::Init($config);
			$this->miniapp = EasyWechat::miniapp();
		}

		protected function Code($scene, $path, $width = 200, $is_show = 1)
		{
			$optional['page'] = $path;
			$optional['width'] = $width;
			$optional['is_hyaline'] = true;
			$lenth = strlen($scene);
			if ($lenth > 32)
			{
				result("scene数据长度为{$lenth}，超过了32位长度");
			}

			$imge_path = RUNTIME_PATH . 'temp/';
			$fileName = 'appcode.png';
//			if (file_exists($imge_path . $fileName))
//			{
//				unlink($imge_path . $fileName);
//			}


			$response = $this->miniapp->app_code->getUnlimit($scene, $optional);
			$response->saveAs($imge_path, $fileName);
			if ($is_show)
			{
				$this->showImg($imge_path . $fileName);
			} else
			{
				return $imge_path . $fileName;
			}
		}

		/**
		 * 生成小程序二维码B接口
		 * api    POST //store/shareimg/getunlimit
		 * @param array $optional 微信参数
		 * @param array $path 微信页面地址
		 * @param bool $is_show 是否直接输出图片0输出图片存储地址1输出图片
		 * @return string    $img 图片资源(直接src调用)
		 */
		public function getunlimit()
		{
			$is_show = $this->request->param('is_show', 0);
			$optional = $this->request->param('optional', '');
			if ($optional == '')
			{
				$optional = [];
			}
			$path = $this->request->param('path', '');


			if ($path == '')
			{
				$this->error('path');
			}
			$params = $this->request->param();
			if (isset($params['path']))
			{
				unset($params['path']);
			}

			$scene = UtilStr::arrayToString($params, '=', '&');

			$optional['path'] = $path;
			$optional['width'] = 200;
//			$optional['is_hyaline'] = true;

			return $this->Code($scene, $path, 200, $is_show);

		}

		/**
		 * 商品分享图(参数包含小程序码B接口)
		 * api    POST //store/Shareimg/shareproduct
		 * @param integer $goods_id 商品ID
		 * @param string $scene 微信scene
		 * @param array $optional 微信参数
		 * @param array $path 微信页面地址
		 * @param bool $is_show 是否直接输出图片0输出图片存储地址1输出图片
		 * @return string    $img 图片资源(直接src调用)
		 * @throws \think\db\exception\DataNotFoundException
		 * @throws \think\db\exception\ModelNotFoundException
		 * @throws \think\exception\DbException
		 */
		public function shareproduct()
		{
			trace(time() . '开始记录', 'info');
			$goods_id = $this->request->param('goods_id', 0);

			$path = $this->request->param('path', '');
			$params = $this->request->param();
			if (isset($params['path']))
			{
				unset($params['path']);
			}

			$scene = UtilStr::arrayToString($params, '=', '&');


			if ($goods_id == 0)
			{
				$this->error('缺少 goods_id 参数！');
			}

			$product = \app\store\model\Store::where(['goods_id' => $goods_id])->find();

			$product['goods_image'] = get_file_path($product['goods_image']);

			if (strpos($product['goods_image'], request()->domain()) !== false)
			{
				//dump([request()->domain().request()->baseFile(),$product['goods_image'],request()->domain(), ROOT_PATH, $goods_image]);
				$goods_image = str_ireplace(request()->domain(), PUBLIC_PATH, $product['goods_image']);
				$product['goods_image'] = str_ireplace('//', '/', $goods_image);
			}


			$gData = [
				'pic'   => $product['goods_image'],
				'title' => $product['goods_name'],
				'price' => $product['goods_store_price'],
			];

			$getUnlimit = $this->Code($scene, $path, 200, 0);
			trace([
				$gData,
				$getUnlimit,
				$this->request->param()
			], 'info');
			trace(time() . '打印结束', 'info');
			UtilImgeProductQrcode::createSharePng($gData, $getUnlimit);
		}

		protected function showImg($img)
		{
			$info = getimagesize($img);
			$imgExt = image_type_to_extension($info[2], false);  //获取文件后缀
			$fun = "imagecreatefrom{$imgExt}";
			$imgInfo = $fun($img);                  //1.由文件或 URL 创建一个新图象。如:imagecreatefrompng ( string $filename )
			//$mime = $info['mime'];
			$mime = image_type_to_mime_type(exif_imagetype($img)); //获取图片的 MIME 类型
			header('Content-Type:' . $mime);
			$quality = 100;
			if ($imgExt == 'png')
				$quality = 9;      //输出质量,JPEG格式(0-100),PNG格式(0-9)
			$getImgInfo = "image{$imgExt}";
			$getImgInfo($imgInfo, null, $quality);  //2.将图像输出到浏览器或文件。如: imagepng ( resource $image )
			die;


		}
	}